#ifndef TRAFFICSIGNAL_H
#define TRAFFICSIGNAL_H

#include <QList>
#include "streetmap.h"

// A traffic signal is one traffic light, it can have one state at any given time.
class TrafficSignal
{
  public:
    enum SIGNAL_STATE {SIGNAL_NONE=0, SIGNAL_RED, SIGNAL_YELLOW, SIGNAL_GREEN};
    enum LIGHT_POSITION {LIGHT_POS_LEFT, LIGHT_POS_RIGHT, LIGHT_POS_STRAIGHT};
    class LightTimeSet
    {
    public:
      qreal time;
      SIGNAL_STATE state;
    };

    TrafficSignal();
    void setFrom(const StreetMap::Road* r);
    void setTo(const StreetMap::Road* r);
    void setIntersection(const StreetMap::Intersection* i);
    int& lane();
    const StreetMap::Road *from() const;
    const StreetMap::Road *to() const;
    const StreetMap::Intersection *intersection() const;
    int lane() const;
    QString name() const;
    const QList<LightTimeSet>& straightLightTimes() const;
    const QList<LightTimeSet>& rightLightTimes() const;
    const QList<LightTimeSet>& leftLightTimes() const;
    void removeLightTime(int pos, LIGHT_POSITION lp);
    void addLightTime(SIGNAL_STATE state, qreal timeInSecs, LIGHT_POSITION lp);
    void addLightTime(SIGNAL_STATE state, qreal timeInSecs, int pos, LIGHT_POSITION lp);
    SIGNAL_STATE getState(LIGHT_POSITION pos) const;
    void tick();

    static QString SignalStateToStr(SIGNAL_STATE state);

  protected:
    int mLane;
    const StreetMap::Intersection *mIntersection;
    const StreetMap::Road *mFromRoad, *mToRoad;
    QList<LightTimeSet> mStraightLightTimes, mRightLightTimes, mLeftLightTimes;
    int mCurrentLeftStatePos, mCurrentRightStatePos, mCurrentStraightStatePos;
    qreal mElapsedTimeInLeftState, mElapsedTimeInStraightState, mElapsedTimeInRightState;
};

#endif // TRAFFICSIGNAL_H
